package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 * Created by guluwa on 2018/12/3.
 */
class MeViewModel : BaseViewModel() {

    private var userLogOutFresh = MutableLiveData<FreshBean>()

    private var userLogOutResult: LiveData<ViewDataBean<ResultBean<Any>>>? = null

    fun userLogOut(): LiveData<ViewDataBean<ResultBean<Any>>>? {
        if (userLogOutResult == null) {
            userLogOutResult = Transformations.switchMap(userLogOutFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().userLogOut(it.map)
                } else {
                    null
                }
            }
        }
        return userLogOutResult!!
    }

    fun freshUserLogOut(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) userLogOutFresh.value = FreshBean(map, isFresh)
    }

    private var userCenterFresh = MutableLiveData<FreshBean>()

    private var userCenterResult: LiveData<ViewDataBean<RemoteResultBean<UserCenterBean>>>? = null

    fun userCenter(): LiveData<ViewDataBean<RemoteResultBean<UserCenterBean>>>? {
        if (userCenterResult == null) {
            userCenterResult = Transformations.switchMap(userCenterFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().userCenter(it.map)
                } else {
                    null
                }
            }
        }
        return userCenterResult!!
    }

    fun freshUserCenter(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) userCenterFresh.value = FreshBean(map, isFresh)
    }

    private var getUserTeamInfoFresh = MutableLiveData<FreshBean>()

    private var getUserTeamInfoResult: LiveData<ViewDataBean<RemoteResultBean<UserTeamInfoBean>>>? = null

    fun getUserTeamInfo(): LiveData<ViewDataBean<RemoteResultBean<UserTeamInfoBean>>>? {
        if (getUserTeamInfoResult == null) {
            getUserTeamInfoResult = Transformations.switchMap(getUserTeamInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getUserTeamInfo(it.map)
                } else {
                    null
                }
            }
        }
        return getUserTeamInfoResult!!
    }

    fun freshGetUserTeamInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getUserTeamInfoFresh.value = FreshBean(map, isFresh)
    }

    private var getUserTeamListFresh = MutableLiveData<FreshBean>()

    private var getUserTeamListResult: LiveData<ViewDataBean<RemoteResultBean<UserTeamMemberBean>>>? = null

    fun getUserTeamList(): LiveData<ViewDataBean<RemoteResultBean<UserTeamMemberBean>>>? {
        if (getUserTeamListResult == null) {
            getUserTeamListResult = Transformations.switchMap(getUserTeamListFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getUserTeamList(it.map)
                } else {
                    null
                }
            }
        }
        return getUserTeamListResult!!
    }

    fun freshGetUserTeamList(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getUserTeamListFresh.value = FreshBean(map, isFresh)
    }

    private var getUserProfitFresh = MutableLiveData<FreshBean>()

    private var getUserProfitResult: LiveData<ViewDataBean<RemoteResultBean<UserProfitBean>>>? = null

    fun getUserProfit(): LiveData<ViewDataBean<RemoteResultBean<UserProfitBean>>>? {
        if (getUserProfitResult == null) {
            getUserProfitResult = Transformations.switchMap(getUserProfitFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getUserProfit(it.map)
                } else {
                    null
                }
            }
        }
        return getUserProfitResult!!
    }

    fun freshGetUserProfit(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getUserProfitFresh.value = FreshBean(map, isFresh)
    }

    private var getProfitRecordsFresh = MutableLiveData<FreshBean>()

    private var getProfitRecordsResult: LiveData<ViewDataBean<RemoteResultBean<ProfitRecordsBean>>>? = null

    fun getProfitRecords(): LiveData<ViewDataBean<RemoteResultBean<ProfitRecordsBean>>>? {
        if (getProfitRecordsResult == null) {
            getProfitRecordsResult = Transformations.switchMap(getProfitRecordsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getProfitRecords(it.map)
                } else {
                    null
                }
            }
        }
        return getProfitRecordsResult!!
    }

    fun freshGetProfitRecords(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getProfitRecordsFresh.value = FreshBean(map, isFresh)
    }

    private var getWithdrawRecordsFresh = MutableLiveData<FreshBean>()

    private var getWithdrawRecordsResult: LiveData<ViewDataBean<RemoteResultBean<WithdrawRecordsBean>>>? = null

    fun getWithdrawRecords(): LiveData<ViewDataBean<RemoteResultBean<WithdrawRecordsBean>>>? {
        if (getWithdrawRecordsResult == null) {
            getWithdrawRecordsResult = Transformations.switchMap(getWithdrawRecordsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getWithdrawRecords(it.map)
                } else {
                    null
                }
            }
        }
        return getWithdrawRecordsResult!!
    }

    fun freshGetWithdrawRecords(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getWithdrawRecordsFresh.value = FreshBean(map, isFresh)
    }

    private var getAlipayInfoFresh = MutableLiveData<FreshBean>()

    private var getAlipayInfoResult: LiveData<ViewDataBean<RemoteResultBean<AlipayInfoBean>>>? = null

    fun getAlipayInfo(): LiveData<ViewDataBean<RemoteResultBean<AlipayInfoBean>>>? {
        if (getAlipayInfoResult == null) {
            getAlipayInfoResult = Transformations.switchMap(getAlipayInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getAlipayInfo(it.map)
                } else {
                    null
                }
            }
        }
        return getAlipayInfoResult!!
    }

    fun freshGetAlipayInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getAlipayInfoFresh.value = FreshBean(map, isFresh)
    }

    private var sendCheckCodeFresh = MutableLiveData<FreshBean>()

    private var sendCheckCodeResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun sendCheckCode(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (sendCheckCodeResult == null) {
            sendCheckCodeResult = Transformations.switchMap(sendCheckCodeFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().sendCheckCode(it.map)
                } else {
                    null
                }
            }
        }
        return sendCheckCodeResult!!
    }

    fun freshSendCheckCode(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) sendCheckCodeFresh.value = FreshBean(map, isFresh)
    }

    private var bindAlipayFresh = MutableLiveData<FreshBean>()

    private var bindAlipayResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun bindAlipay(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (bindAlipayResult == null) {
            bindAlipayResult = Transformations.switchMap(bindAlipayFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().bindAlipay(it.map)
                } else {
                    null
                }
            }
        }
        return bindAlipayResult!!
    }

    fun freshBindAlipay(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) bindAlipayFresh.value = FreshBean(map, isFresh)
    }

    private var addWithdrawApplyFresh = MutableLiveData<FreshBean>()

    private var addWithdrawApplyResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun addWithdrawApply(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (addWithdrawApplyResult == null) {
            addWithdrawApplyResult = Transformations.switchMap(addWithdrawApplyFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().addWithdrawApply(it.map)
                } else {
                    null
                }
            }
        }
        return addWithdrawApplyResult!!
    }

    fun freshAddWithdrawApply(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) addWithdrawApplyFresh.value = FreshBean(map, isFresh)
    }

    private var updateUserFresh = MutableLiveData<FreshBean>()

    private var updateUserResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun updateUser(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (updateUserResult == null) {
            updateUserResult = Transformations.switchMap(updateUserFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().updateUser(it.map)
                } else {
                    null
                }
            }
        }
        return updateUserResult!!
    }

    fun freshUpdateUser(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) updateUserFresh.value = FreshBean(map, isFresh)
    }

    private var uploadPicBase64Fresh = MutableLiveData<FreshBean>()

    private var uploadPicBase64Result: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun uploadPicBase64(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (uploadPicBase64Result == null) {
            uploadPicBase64Result = Transformations.switchMap(uploadPicBase64Fresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().uploadPicBase64(it.map)
                } else {
                    null
                }
            }
        }
        return uploadPicBase64Result!!
    }

    fun freshUploadPicBase64(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) uploadPicBase64Fresh.value = FreshBean(map, isFresh)
    }

    private var getUserIsCorrectFresh = MutableLiveData<FreshBean>()

    private var getUserIsCorrectResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun getUserIsCorrect(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (getUserIsCorrectResult == null) {
            getUserIsCorrectResult = Transformations.switchMap(getUserIsCorrectFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getUserIsCorrect(it.map)
                } else {
                    null
                }
            }
        }
        return getUserIsCorrectResult!!
    }

    fun freshGetUserIsCorrect(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getUserIsCorrectFresh.value = FreshBean(map, isFresh)
    }

    private var updatePasswordFresh = MutableLiveData<FreshBean>()

    private var updatePasswordResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun updatePassword(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (updatePasswordResult == null) {
            updatePasswordResult = Transformations.switchMap(updatePasswordFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().updatePassword(it.map)
                } else {
                    null
                }
            }
        }
        return updatePasswordResult!!
    }

    fun freshUpdatePassword(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) updatePasswordFresh.value = FreshBean(map, isFresh)
    }

    private var getAllTagFresh = MutableLiveData<FreshBean>()

    private var getAllTagResult: LiveData<ViewDataBean<RemoteResultBean<List<UserTagBean>>>>? = null

    fun getAllTag(): LiveData<ViewDataBean<RemoteResultBean<List<UserTagBean>>>>? {
        if (getAllTagResult == null) {
            getAllTagResult = Transformations.switchMap(getAllTagFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getAllTag(it.map)
                } else {
                    null
                }
            }
        }
        return getAllTagResult!!
    }

    fun freshGetAllTag(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getAllTagFresh.value = FreshBean(map, isFresh)
    }

    private var saveTagFresh = MutableLiveData<FreshBean>()

    private var saveTagResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun saveTag(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (saveTagResult == null) {
            saveTagResult = Transformations.switchMap(saveTagFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().saveTag(it.map)
                } else {
                    null
                }
            }
        }
        return saveTagResult!!
    }

    fun freshSaveTag(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) saveTagFresh.value = FreshBean(map, isFresh)
    }

    private var uploadMultiPicsFresh = MutableLiveData<FreshBean>()

    private var uploadMultiPicsResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun uploadMultiPics(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (uploadMultiPicsResult == null) {
            uploadMultiPicsResult = Transformations.switchMap(uploadMultiPicsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().uploadMultiPics(it.map)
                } else {
                    null
                }
            }
        }
        return uploadMultiPicsResult!!
    }

    fun freshUploadMultiPics(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) uploadMultiPicsFresh.value = FreshBean(map, isFresh)
    }

    private var addFeedbackFresh = MutableLiveData<FreshBean>()

    private var addFeedbackResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun addFeedback(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (addFeedbackResult == null) {
            addFeedbackResult = Transformations.switchMap(addFeedbackFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().addFeedback(it.map)
                } else {
                    null
                }
            }
        }
        return addFeedbackResult!!
    }

    fun freshAddFeedback(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) addFeedbackFresh.value = FreshBean(map, isFresh)
    }

    private var getCommonProblemsFresh = MutableLiveData<FreshBean>()

    private var getCommonProblemsResult: LiveData<ViewDataBean<RemoteResultBean<List<CommonProblemBean>>>>? = null

    fun getCommonProblems(): LiveData<ViewDataBean<RemoteResultBean<List<CommonProblemBean>>>>? {
        if (getCommonProblemsResult == null) {
            getCommonProblemsResult = Transformations.switchMap(getCommonProblemsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getCommonProblems(it.map)
                } else {
                    null
                }
            }
        }
        return getCommonProblemsResult!!
    }

    fun freshGetCommonProblems(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getCommonProblemsFresh.value = FreshBean(map, isFresh)
    }

    private var getCollectListFresh = MutableLiveData<FreshBean>()

    private var getCollectListResult: LiveData<ViewDataBean<RemoteResultBean<CollectListBean>>>? = null

    fun getCollectList(): LiveData<ViewDataBean<RemoteResultBean<CollectListBean>>>? {
        if (getCollectListResult == null) {
            getCollectListResult = Transformations.switchMap(getCollectListFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getCollectList(it.map)
                } else {
                    null
                }
            }
        }
        return getCollectListResult!!
    }

    fun freshGetCollectList(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getCollectListFresh.value = FreshBean(map, isFresh)
    }

    private var getOrderListFresh = MutableLiveData<FreshBean>()

    private var getOrderListResult: LiveData<ViewDataBean<RemoteResultBean<TaobaoOrderBean>>>? = null

    fun getOrderList(): LiveData<ViewDataBean<RemoteResultBean<TaobaoOrderBean>>>? {
        if (getOrderListResult == null) {
            getOrderListResult = Transformations.switchMap(getOrderListFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getOrderList(it.map)
                } else {
                    null
                }
            }
        }
        return getOrderListResult!!
    }

    fun freshGetOrderList(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getOrderListFresh.value = FreshBean(map, isFresh)
    }

    private var cancelCollectFresh = MutableLiveData<FreshBean>()

    private var cancelCollectResult: LiveData<ViewDataBean<RemoteResultBean<Any>>>? = null

    fun cancelCollect(): LiveData<ViewDataBean<RemoteResultBean<Any>>>? {
        if (cancelCollectResult == null) {
            cancelCollectResult = Transformations.switchMap(cancelCollectFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().cancelCollect(it.map)
                } else {
                    null
                }
            }
        }
        return cancelCollectResult!!
    }

    fun freshCancelCollect(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) cancelCollectFresh.value = FreshBean(map, isFresh)
    }

    private var getRecommendPageFresh = MutableLiveData<FreshBean>()

    private var getRecommendPageResult: LiveData<ViewDataBean<RemoteResultBean<UserCenterGoodBean>>>? = null

    fun getRecommendPage(): LiveData<ViewDataBean<RemoteResultBean<UserCenterGoodBean>>>? {
        if (getRecommendPageResult == null) {
            getRecommendPageResult = Transformations.switchMap(getRecommendPageFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getRecommendPage(it.map)
                } else {
                    null
                }
            }
        }
        return getRecommendPageResult!!
    }

    fun freshGetRecommendPage(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getRecommendPageFresh.value = FreshBean(map, isFresh)
    }
}